angular.module('FirstCheck')
    .controller('FirstCheck.FirstCheckCtrl', ['$scope', '$state', '$http', '$ionicLoading', '$ionicPopup', '$timeout', 'Http', 'imanDialog', 'imanUsbPort', 'User','Device',
        function($scope, $state, $http, $ionicLoading, $ionicPopup, $timeout, Http, imanDialog, imanUsbPort, User,Device) {
            $scope.data = {
                isCheck: false,
                getQc: false,
                Barcode: '',
                localConfig: {},
                User: User.get(),
                device : Device.get()
            };


            $scope.$on('$ionicView.beforeEnter', function() {
                imanUsbPort.start();
            });

            $scope.$on('imanUsbPort.input', function(e, inputStr) {
                $scope.data.Barcode = inputStr;
                getQcInfo($scope.data.Barcode);
            });

            $scope.submit = function() {
                $ionicLoading.show();
                $scope.data.User.IsQC = true;
                Http.post({
                    url: '/api/FirstQC/setFirstQCInfo',
                    data: {
                        Parameter: {
                            User: $scope.data.User,
                            Terminal: $scope.data.device.Terminal,
                            WorkLocation: $scope.data.device.WorkLocation,
                            Work: $scope.data.device.Work,
                            BarcodeMaster: $scope.data.BarcodeMaster,
                            BizRecord: $scope.data.BizRecord,
                            QCRecords: $scope.data.QCRecords
                        }
                    }
                }).then(function() {
                    $ionicLoading.hide();
                    $scope.data.getQc = true;
                    imanDialog.toastBottom('确认成功');
                    $timeout(function() {
                        imanDialog.toastTop('请扫描单品信息');
                    }, 2000);
                }, function(err) {
                    $ionicLoading.hide();
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            };

            function getQcInfo(Barcode) {
                $ionicLoading.show();
                Http.post({
                    url: '/api/FirstQC/getFirstQCDefaultInfo',
                    data: {
                        Parameter: {
                            Barcode: Barcode,
                            WorkID: $scope.data.device.Work._id
                        }
                    }
                }).then(function(info) {
                    operateBarCode(info);
                })['finally'](function() {
                    $ionicLoading.hide();
                });
            }

            $scope.$on('$ionicView.afterLeave', function() {
                imanUsbPort.stop();
            });

            function operateBarCode(info) {
                $scope.data.MO = info.BarcodeMaster.MO;
                $scope.data.BarcodeMaster = info.BarcodeMaster;
                $scope.data.BizRecord = info.BizRecord;
                $scope.data.QCRecords = info.QCRecords;
                $scope.data.QCNorm = info.QCNorm;
                $scope.data.Work = info.BarcodeMaster.Work;
                $scope.data.Remark = info.Remark;

                angular.forEach($scope.data.QCRecords, function(value) {
                    if (value.NormValue === 'false') {
                        value.NormValue = false;
                    } else if (value.NormValue === 'true') {
                        value.NormValue = true;
                    } else {

                    }
                });
            }
        }
    ]);
